import Vue from 'vue'
import infoDialog from '@/components/alert.vue'
import Bus from '@/common/js/vueBus.js';
const infoPop = Vue.extend(infoDialog);

export default {
    showAlert : function (message) {
        var self =this;
        var options = {
            el: document.createElement('div'),
            data:{
                succ : false,
                classType : "fa-times-circle",
                text:message
            }
        };

        // awaitPop.push(options);

        self.pushIntoStack(options);
            

        //监听用户鼠标移入错误提示框事件
        Bus.$on('clearInterval', (target,count) => {  
            if (target === true) {
                window.clearTimeout(self.alertTimeout);
                console.log("enter",count);
                self.alertTimeout = null;
            } else if (target === "close") {
                document.body.removeChild(self.instancePop.$el);
                Vue.nextTick(function(){
                    self.instancePop.$destroy();
                    self.instancePop = null;
                })
            } else {
                console.log("out",count);
                self.alertTimeout = setTimeout(function(){
                    if (self.instancePop) {
                        document.body.removeChild(self.instancePop.$el);
                        Vue.nextTick(function(){
                            self.instancePop.$destroy();
                            self.instancePop = null;
                        })                               
                    }
                },(count / 100)*2000)  
            }        
        }); 
    },
    pushIntoStack:function (options) {
        var self = this;
        if (!this.instancePop) {
            this.instancePop = new infoPop(options);
            document.body.appendChild(this.instancePop.$el);
            this.alertTimeout = setTimeout(function(){
                self.destroyInstance();
            },2000)
        } else {
            window.clearTimeout(self.alertTimeout);
            self.destroyInstance();
            // Vue.nextTick(function(){

            // })
            self.pushIntoStack(options);
        }

    },
    destroyInstance:function(){
        var self = this;
        document.body.removeChild(self.instancePop.$el);
        // Vue.nextTick(function(){
            self.instancePop.$destroy();
            self.instancePop = null;
            // if (awaitPop.length > 0) {

            // }
        // })      
    },
    alertTimeout:null,
    instancePop:null,
    awaitPop:[]

}